using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Documents;
using System.Windows.Controls;

using Spring.Context;
using Spring.Context.Support;

using Molic.Core.ActionHandlers;

namespace Molic.Main
{
    /// <summary>
    /// Converts button names of the ribbon to ICommand objects.
    /// </summary>
    public class MolicCommandConverter : IMultiValueConverter
    {
        public Object Convert(Object[] value, Type targetType, Object parameter, System.Globalization.CultureInfo culture)
        {
            String name = value[0] as String;

            try
            {
                IApplicationContext ctx = ContextRegistry.GetContext();
                IMolicActionHandler tool = (IMolicActionHandler)ctx.GetObject(name);
                Button button = value[1] as Button;
                button.CommandBindings.Add(new CommandBinding(tool.Command, tool.OnExecute, tool.OnQueryEnabled));
                return tool.Command;
            }
            catch (Spring.Objects.Factory.NoSuchObjectDefinitionException)
            {
                //TODO: put some logging code here dude!!
                return null;
            }
            catch (Spring.Objects.ObjectsException)
            {
                //TODO: and here too bro!
                return null;
            }
            #region old code
            //switch (name)
            //{
            //    #region Tools
            //    case "CreateConnection":
            //        button = value[1] as Button;
            //        tool = MolicToolManager.FindTool("Create Connection");
            //        if (tool != null)
            //        {
            //            button.CommandBindings.Add(new CommandBinding(tool.Command, tool.OnExecute, tool.OnQueryEnabled));
            //            return tool.Command;
            //        }
            //        return null;
            //    case "CreateMolicScene":
            //        button = value[1] as Button;
            //        tool = MolicToolManager.FindTool("Create Molic Scene");
            //        if (tool != null)
            //        {
            //            button.CommandBindings.Add(new CommandBinding(tool.Command, tool.OnExecute, tool.OnQueryEnabled));
            //            return tool.Command;
            //        }
            //        return null;
            //    case "CreateConversationClosingPoint":
            //        button = value[1] as Button;
            //        tool = MolicToolManager.FindTool("Create Conversation Closing Point");
            //        if(tool!=null)
            //        {
            //            button.CommandBindings.Add(new CommandBinding(tool.Command, tool.OnExecute, tool.OnQueryEnabled));
            //            return tool.Command;
            //        }
            //        return null;

            //    case "CreateConversationOpenningPoint":
            //        button = value[1] as Button;
            //        tool = MolicToolManager.FindTool("Create Conversation Opening Point");
            //        if (tool != null)
            //        {
            //            button.CommandBindings.Add(new CommandBinding(tool.Command, tool.OnExecute, tool.OnQueryEnabled));
            //            return tool.Command;
            //        }
            //        return null;
            //    case "ToggleItalic":
            //        button = value[1] as Button;
            //        tool = MolicToolManager.FindTool("Make italic");
            //        if (tool != null)
            //        {
            //            button.CommandBindings.Add(new CommandBinding(tool.Command, tool.OnExecute, tool.OnQueryEnabled));
            //            return tool.Command;
            //        }
            //        return null;
            //        // could have used the predefined EditingCommands.ToggleItalic;
            //    case "ToggleUnderline":
            //        button = value[1] as Button;
            //        tool = MolicToolManager.FindTool("Make underline");
            //        if (tool != null)
            //        {
            //            button.CommandBindings.Add(new CommandBinding(tool.Command, tool.OnExecute, tool.OnQueryEnabled));
            //            return tool.Command;
            //        }
            //        return null;
            //    #endregion

            //    #region Editing Commands
            //    case "AlignLeft":
            //        return EditingCommands.AlignLeft;
            //    case "AlignCenter":
            //        return EditingCommands.AlignCenter;
            //    case "AlignRight":
            //        return EditingCommands.AlignRight;
            //    case "AlignJustify":
            //        return EditingCommands.AlignJustify;
            //    case "IncreaseIndentation":
            //        return EditingCommands.IncreaseIndentation;
            //    case "DecreaseIndentation":
            //        return EditingCommands.DecreaseIndentation;
            //    case "ToggleBullets":
            //        return EditingCommands.ToggleBullets;
            //    case "ToggleNumbering":
            //        return EditingCommands.ToggleNumbering;
            //    #endregion
            //    #region Do-Undo
            //    case "Undo":
            //        button = value[1] as Button;
            //        button.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, MolicUndoManager.OnUndoExecute, MolicUndoManager.OnQueryUndoEnabled));
            //        return ApplicationCommands.Undo;
            //    case "Redo":
            //        button = value[1] as Button;
            //        button.CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, MolicUndoManager.OnRedoExecute, MolicUndoManager.OnQueryRedoEnabled));
            //        return ApplicationCommands.Redo;
            //    default:
            //        return null;
            //    #endregion
            //}
            #endregion
        }

        public Object[] ConvertBack(Object value, Type[] targetType, Object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
